import globalConfig from 'src/util/Global';
import { API } from '@/constants/api';
import { graphHelper } from 'src/api/GraphHelper';
import { GraphNodeStatus } from './../../constants/Enums';
import { treeForEach } from 'src/util/common';
class GraphNodeStatusFresher {
  interval = 10
  timer: NodeJS.Timeout
  freshNodeStatus() {
    if (!globalConfig.token) return;
    if (!app.activeProject?.config.online) return;
    const param = {
      projectId: app.activeProject.config.id,
      checkOutId: []
    };
    param.checkOutId = this.getAllPackageStatus([app.sysmlModel]);
    //// 如果是通过md3/md3t文件启动, 则自动打开工程
    app.$post(API.PLATFORM_SERVICE_COOP_FETCH, param).then((res) => {
      const resData = res.data;
      resData.forEach((item) => {
        const node = graphHelper.getGraphNodeById(item.packageId, app.activeProjectId);
        if (node) {
          node.status = item.flag;
          //// 同步nodeStatus
          let nodeStatus;
          if (!app.activeProject.onlineConfig.nodeStatus){
            nodeStatus = {};
            nodeStatus[node.id] = item.flag;
            app.activeProject.onlineConfig.nodeStatus = nodeStatus;
          } else {
            app.activeProject.onlineConfig.nodeStatus[node.id] = item.flag;
          }
          //// 同步到模型树
          treeForEach([app.sysmlModel], (node) => {
            if (node.id === item.packageId) {
              node.status = item.flag;
            }
          });
        }
      });
      this.setNodeStatus([app.sysmlModel]);
    }).catch((err) => {
      console.log(err);
    });
  }
  setNodeStatus(nodes: GraphNode[]) {
    //// 设置子节点的属性随父节点
    nodes.forEach(node => {
      if (node && node.getTypeName()) {
        const typeName = node.getTypeName() || '';
        if ( !(['Package', 'Model', 'Profile'].includes(typeName)) ){
          node.status = node.getParentNode().status;
          if (app.activeProject.onlineConfig.nodeStatus && app.activeProject.onlineConfig.nodeStatus[node.id]) {
            app.activeProject.onlineConfig.nodeStatus[node.id] = node.getParentNode().status;
          }
        }
        if (node.children && node.children.length) {
          this.setNodeStatus(node.children);
        }
      }
    });
  }
  startTimer() {
    clearInterval(this.timer);
    this.freshNodeStatus();
    this.timer = setInterval(() => {
      this.freshNodeStatus();
    }, this.interval * 1000);

  }
  stopTimer() {
    clearInterval(this.timer);
    this.timer = null;
  }
  getAllPackageStatus(nodes: GraphNode[], pkgStatus = []) {
    nodes.forEach(node => {
      if (node && node.getTypeName()) {
        const typeName = node.getTypeName() || '';
        if (typeName === 'Package' || typeName === 'Model' || (typeName === 'Profile' && node.status !== GraphNodeStatus.FROM_PROFILE)) {
          //// node.status = app.activeProject.onlineConfig.checkoutIds && app.activeProject.onlineConfig.checkoutIds.find(i => i.packageId === node.id).status;
          if (!(node.status === GraphNodeStatus.DEFAULT || node.status === GraphNodeStatus.FROM_PROFILE )) {
            pkgStatus.push({ packageId: node.id });
          }
        } 
        if (node.children && node.children.length) {
          this.getAllPackageStatus(node.children, pkgStatus);
        }
      }
    });
    return pkgStatus;
  }
}
const graphNodeStatusFresher = new GraphNodeStatusFresher();
export default graphNodeStatusFresher;